<?
/*
@Name: MapInfos
@Author: Demonaz
@Year: 2006
@Contact: bruce.feuillette(at)gmail.com

This Class allows you to have all the informations about a TrackMania map.
Including the possibilities to create its thumbnail.

To Do
- decoding the nickname colors and font decoration.
- decoding the map's name in the same way

15:05 28/11/2006: file_get_contents used instead of readfile

*/
//Function to make the time readable, with this format: mm'ss"00
function humantime($time)
	{
	$mm=$time%1000;
	$time=strftime("%M:%S",($time/1000));
	return $time.="'".$mm;
	}


//Beginning of the class
class MapInfos {
	var $name; //Name of the map, without the .Challenge.Gbx. The path could be relative to the script.
	var $path; //Path to the map
	var $nadeo; //Author (called Nadeo) record: time or tries depending of the challenge type in this format: mm'ss"00
	var $gold; //Gold medal
	var $silver; //Silver medal
	var $bronze; //Copper medal
	var $authortime; //Author time, always here even in Plateform or Stunts maps
	var $raw_nadeo; //Raw Nadeo time, in thousandth of seconds
	var $raw_gold; //Raw Gold time
	var $raw_silver; //Raw Silver time
	var $raw_bronze; //Raw Copper time
	var $raw_authortime; //Raw Authortime
	var $author; //Author name
	var $mood; //Mood used by the map: Sunset, Day...
	var $environment; //Environnement used by the map: Rallye, Stadium...
	var $nblaps; //Number of laps if it's a Lap Map
	var $price; //Graphical cost of the map
	var $type; //Type of challenge
	var $ingame_name; //Real name of the map
	var $formated_name; //Formated map name

function name($map_name,$map_path)
	{
    $this->name=$map_name;
	$this->path=$map_path;
 	}

function infos()
	{
	$filename=$this->path.$this->name.'.Challenge.Gbx';
    $buffer=file_get_contents($filename);
	$start=strpos($buffer,"<header");
	$end=strpos($buffer,"</header>")+9;
	$end=$end-$start;
	$header=substr($buffer,$start,$end);
	$p = xml_parser_create();
	xml_parse_into_struct($p, $header, $values);
	xml_parser_free($p);
	$this->author=$values['1']['attributes']['AUTHOR'];
	//Do not use, conflict with the type of the challenge
	//$this->type=$values['0']['attributes']['TYPE'];
	$this->version=$values['0']['attributes']['VERSION'];
	$this->exever=$values['0']['attributes']['EXEVER'];
	$this->uid=$values['1']['attributes']['UID'];
	$this->ingame_name=$values['1']['attributes']['NAME'];
	$this->raw_gold=$values['3']['attributes']['GOLD'];
	$this->raw_silver=$values['3']['attributes']['SILVER'];
	$this->raw_bronze=$values['3']['attributes']['BRONZE'];
	$this->raw_nadeo=$values['3']['attributes']['AUTHORSCORE'];
	$this->raw_authortime=$values['3']['attributes']['AUTHORTIME'];
	$this->environment=$values['2']['attributes']['ENVIR'];
	$this->mood=$values['2']['attributes']['MOOD'];
	$this->type=$values['2']['attributes']['TYPE'];
	$this->nblaps=$values['2']['attributes']['NBLAPS'];
	$this->price=$values['2']['attributes']['PRICE'];
	//We're generating readable human time, only when needed
	if(($this->type=='Platform')||($this->type=='Stunts'))
		{
		$this->gold=$this->raw_gold;
		$this->silver=$this->raw_silver;
		$this->bronze=$this->raw_bronze;
		//This is an exception, the authorscore is always a time
		$this->authortime=humantime($this->raw_authortime);
		$this->nadeo=$this->raw_nadeo;
		}
	else
		{
		$this->gold=humantime($this->raw_gold);
		$this->silver=humantime($this->raw_silver);
		$this->bronze=humantime($this->raw_bronze);
		$this->nadeo=humantime($this->raw_nadeo);
		$this->authortime=humantime($this->raw_authortime);
		}
	//$this->formated_name=truename($this->ingame_name);
	}

function mapimage()
	{
	header("Content-Type: image/jpeg;");
	$filename=$this->path.$this->name.'.Challenge.Gbx';
	$buffer=file_get_contents($filename);
	//Search for the <Thumbnail.jpg> tag
	$start=strpos($buffer,"<Thumbnail.jpg>");
	//Search for the <Thumbnail.jpg> tag
	$end=strpos($buffer,"</Thumbnail.jpg>");
	//The start offset is without the <Thumbnail.jpg> tag
	$start=$start+15;
	//Length of the code of the image
	$end=$end-$start;
	$image=substr($buffer,$start,$end);
	//Creation of a GD image from the string.
	$image=imagecreatefromstring($image);
	//We collect the width and height
	$width=imagesx($image);
	$height=imagesy($image);
	//We create a fake image to flip vertically the original one into
	$end_image=imagecreatetruecolor($width, $height);
	//We copy pixel by pixel to flip
	for ($x = 0; $x < $width; $x++)
		{
		for ($y = 0; $y < $height; $y++)
			{
			imagecopy($end_image, $image, $x, $height - $y - 1, $x, $y, 1, 1);
			}
		}
	return imagejpeg($end_image);
	}
//End of MapInfos Class
}
?>
